package cn.qn.service.impl.user;

import cn.qn.base.PageVO;
import cn.qn.dto.user.AppUserDTO;
import cn.qn.dto.user.AppUserQueryDTO;
import cn.qn.dto.user.AppUserScoreDTO;
import cn.qn.dto.user.AppUserScoreQueryDTO;
import cn.qn.enums.AppUserScoreSourceEnum;
import cn.qn.enums.AppUserSexEnum;
import cn.qn.enums.AppUserStatusEnum;
import cn.qn.enums.AppUserTypeEnum;
import cn.qn.exceptions.BizException;
import cn.qn.mapper.user.AppUserMapper;
import cn.qn.mapper.user.AppUserScoreMapper;
import cn.qn.moudle.user.AppUser;
import cn.qn.moudle.user.AppUserScore;
import cn.qn.repository.user.AppUserRepository;
import cn.qn.repository.user.AppUserScoreRepository;
import cn.qn.service.user.AppUserScoreService;
import cn.qn.utils.DateUtils;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Auther: Bryant
 * @Date: 2018/6/11
 * @Description:
 */
@com.alibaba.dubbo.config.annotation.Service(interfaceClass = AppUserScoreService.class)
@Service
public class AppUserScoreServiceImpl implements AppUserScoreService {

    @Resource
    private AppUserScoreMapper appUserScoreMapper;

    @Autowired
    private AppUserScoreRepository appUserScoreRepository;

    @Autowired
    private AppUserRepository appUserRepository;
    /**
     * 最多连续签到天数
     */
    private static Integer MAX_COUNTINUED_DAYS=7;

    /**
     * 每日领取积分系数
     */
    private static Integer ONE_DAY_SCORE_RATIO=1;

    @Override
    public PageVO<AppUserScoreDTO> pageQueryAppUserScores(AppUserScoreQueryDTO query) {
        PageBounds pb = new PageBounds(query.getPageBounds().getPage(), query.getPageBounds().getLimit());
        List<AppUserScoreDTO> list = appUserScoreMapper.pageQueryAppUserScores(query, pb);
        list.forEach(dto -> {
            dto.setShowSourceType(AppUserScoreSourceEnum.getDesc(dto.getSourceType()));

        });
        Integer count = appUserScoreMapper.pageQueryAppUserScoresCount(query);
        return PageVO.convert(pb.getPage(), pb.getLimit(), list, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer checkIn(String userId) throws BizException{
        //判断今日是否已经签到
        AppUserScore lastCheckIn = appUserScoreMapper.findLastOneByUserId(userId);
        Integer todayScore=0;
        Integer continuedDays=1;
        if(lastCheckIn==null){
            todayScore = 1*ONE_DAY_SCORE_RATIO;
        }
        else if(DateUtils.isSameDay(lastCheckIn.getCreateTime(),new Date())){
            throw new BizException("今日已签到");
        }
        //计算今日可领取积分数
        //判断是否是昨日签到的
        else if(DateUtils.isSameDay(lastCheckIn.getCreateTime(),DateUtils.addDays(new Date(),-1))){
            if(lastCheckIn.getContinuedDays()>=MAX_COUNTINUED_DAYS){
                //返回连续签到的最高积分
                todayScore = MAX_COUNTINUED_DAYS * ONE_DAY_SCORE_RATIO;
                continuedDays=lastCheckIn.getContinuedDays()+1;
            }else{
                //返回持续天数的积分
                todayScore = (lastCheckIn.getContinuedDays()+1)*ONE_DAY_SCORE_RATIO;
                continuedDays=lastCheckIn.getContinuedDays()+1;
            }
        }else{
            todayScore = 1*ONE_DAY_SCORE_RATIO;
        }
        AppUserScore score=new AppUserScore();
        score.setUserId(userId);
        score.setInScore(todayScore);
        score.setContinuedDays(continuedDays);
        score.setSourceType(AppUserScoreSourceEnum.CHECK_IN.getCode());
        score.setCreateTime(new Date());
        appUserScoreRepository.save(score);
        //更新到账户
        AppUser appUser=appUserRepository.findByUserId(userId);
        appUser.setScore(appUser.getScore()+todayScore);
        appUserRepository.save(appUser);
        return todayScore;
    }

    @Override
    public Boolean checkedIn(String userId) {
        AppUserScore lastCheckIn = appUserScoreMapper.findLastOneByUserId(userId);
        if(null!=lastCheckIn && DateUtils.isSameDay(lastCheckIn.getCreateTime(),new Date())){
           return true;
        }
        return false;
    }

}
